package com.hardydou.system.controller;

import com.github.pagehelper.PageInfo;
import com.hardydou.common.result.Result;
import com.hardydou.common.controller.BaseController;
import com.hardydou.common.result.ResultHelper;
import com.hardydou.framework.log.anno.LogMetaInfo;
import com.hardydou.system.model.SystemRes;
import com.hardydou.system.service.ISystemResService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * @author ：hardy
 * @description：
 * @date ：Created in 2020-03-10
 * @Copyright ：Hardydou.com
 */
@Controller
@RequestMapping("/system/res")
@Api(tags = "资源管理")
@LogMetaInfo(module = "资源管理")
public class SysResController extends BaseController {

    @Autowired
    ISystemResService resService;

    @ApiIgnore
    @GetMapping("")
    public String index() {
        return "res/list";
    }


    @ApiIgnore
    @GetMapping("/add")
    public String add() {
        return "res/add";
    }


    @ApiIgnore
    @PostMapping("/add")
    public String add(SystemRes res, Model model) {
        resService.addRes(res);
        model.addAttribute("code", 200);
        model.addAttribute("message", "添加成功");
        return "res/add";
    }

    @ApiIgnore
    @GetMapping("/edit")
    public String edit(long resId, Model model) {
        SystemRes res = resService.findById(resId);
        SystemRes parent = resService.findById(res.getParentId());
        model.addAttribute("res", res);
        model.addAttribute("parent", parent);
        return "res/edit";
    }

    @ApiIgnore
    @PostMapping("/edit")
    public String edit(SystemRes res, Model model) {
        model.addAttribute("code", 200);
        model.addAttribute("message", "修改成功");
        resService.update(res);
        model.addAttribute("res", res);
        return "res/edit";
    }


    @ApiIgnore
    @GetMapping("/info")
    public String info(long resId, Model model) {
        SystemRes res = resService.findById(resId);
        model.addAttribute("res", res);
        return "res/info";
    }

    @ApiIgnore
    @GetMapping("/tree")
    public String tree(long resId, Model model) {
        SystemRes res = resService.findById(resId);
        model.addAttribute("res", res);
        return "res/tree";
    }


    /* JSON API */

    @GetMapping("/roleTree.json")
    @ResponseBody
    public Result roleTree(long roleId) {
        List ress = resService.findAllByRoleId(roleId);
        return ResultHelper.ok(ress);
    }

    @ApiOperation(value = "资源列表")
    @GetMapping("/list.json")
    @ResponseBody
    public Result<?> listForJson(@RequestParam Map params) {
        params.put("orderBy", "`order` asc");
        List<SystemRes> roles = resService.findAll(params);
        PageInfo pageInfo = new PageInfo(roles);
        return ResultHelper.ok(pageInfo);
    }


    @ApiOperation(value = "添加资源")
    @PostMapping("/add.json")
    @ResponseBody
    public Result<?> addForJson(SystemRes res) {
        resService.addRes(res);
        return ResultHelper.ok(res);
    }


    @ApiOperation(value = "删除资源")
    @ApiImplicitParam(name = "resId", value = "资源ID", required = true)
    @DeleteMapping("/del")
    @ResponseBody
    public Result delete(long resId) {
        resService.deleteByResId(resId);
        Result<Map<String, Object>> result = ResultHelper.ok();
        result.getData().put("resId", resId);
        return result;
    }

    @ApiOperation(value = "更新资源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "resId", value = "资源ID", required = true, paramType = "query")
    })
    @ApiParam(name = "resId", value = "资源ID", required = true)
    @PostMapping("/update")
    @ResponseBody
    public Result update(SystemRes res) {
        resService.update(res);
        return ResultHelper.ok(res);
    }

    @ApiOperation(value = "更新状态")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "resId", value = "资源ID", required = true)
    )
    @PostMapping("/visible")
    @ResponseBody
    public Result visible(long resId) {
        SystemRes res = resService.findById(resId);
        res.setVisible(!res.isVisible());
        resService.update(res);
        Result<Map<String, Object>> result = ResultHelper.ok();
        result.getData().put("resId", resId);
        result.getData().put("visible", res.isVisible());
        return result;
    }

}
